#include "calServer.hpp"
#include <memory>

using namespace server;
using namespace std;

static void Usage(string proc)
{
	std::cout << "\nUsage:\n\t" << proc << ": local_port\n\n";
}

// req:  里面一定是已经处理好的完整的一个请求对象
// resp: 根据req, 进行任务处理, 填充resp, 不宜哦那个管理任何请求与写入, 序列化和反序列化等任何细节
bool cal(const Request &req, Response &resp)
{
	resp.exitcode = OK;
	resp.result = OK;

	switch (req.op)
	{
	case '+':
		resp.result = req.x + req.y;
		break;
	case '-':
		resp.result = req.x - req.y;
		break;
	case '*':
		resp.result = req.x * req.y;
		break;
	case '/':
	{
		if (req.y == 0)
			resp.exitcode = DIV_ZERO;
		else
			resp.result = req.x / req.y;
		break;
	}
	case '%':
	{
		if (req.y == 0)
			resp.exitcode = MOD_ZERO;
		else
			resp.result = req.x % req.y;
		break;
	}
	default:
		resp.exitcode = OP_ERROR;
		break;
	}
	
	return true;
}

int main(int argc, char *argv[])
{
	if (argc != 2)
	{
		Usage(argv[0]);
		exit(1);
	}

	uint16_t port = atoi(argv[1]);

	unique_ptr<CalServer> tsvr(new CalServer());
	tsvr->initServer();

	tsvr->start(cal);

	return 0;
}